$(".item").click(function() {
	$(this).parent().parent().find(".itemSelect").removeClass("itemSelect");
	$(this).addClass("itemSelect");
});
var mySwiper = new Swiper('.swiper-container', {})
function init() {
	var petty_user = localStorage.petty_user;
	if (petty_user) {
		petty_user = JSON.parse(petty_user);
		if (petty_user.isRemember) {
			$("[attid=username]").val(petty_user.username);
			$("[attid=password]").val(petty_user.password).trigger("focus");
			$("#rememberPass").prop("checked", "true");
		}
	}

	var index = $.getUrlVar("index");
	if (index) {
		parent.location.href = "/web/web.html";
	}
	ajaxPost({
		param : JSON.stringify({
			topic : "1",
		})
	}, "/admin/news/list/product",
			function(data) {
				data = data.list;
				var list = data.slice(0, 4);
				$("#product_t1").tmpl(list.shift()).appendTo($(".products"));
				$("#product_t1").tmpl(list[2]).appendTo($(".products"));
				$("#product_t2").tmpl({
					list : list.slice(0, 2)
				}).appendTo($(".products"));
				var width = $("body").width();
				width = parseInt(width);
				if (width < 1200) {
					$(".table").attr("cellspacing", "40").css("top", "-40px");
				} else {
					var len1 = (width - 1080) / 3;
					$(".table").attr("cellspacing", len1).css("top",
							(0 - len1) + "px");
				}

				$(".productitem").hover(function() {
					$(".productitemNow").removeClass("productitemNow");
					$(this).addClass("productitemNow");
					$(".p_moreNow").removeClass("p_moreNow");
					$(this).find(".p_more").addClass("p_moreNow");
				})
			});
	ajaxPost({
		param : "{}"
	}, "/admin/news/list/partner", function(data) {
		var list = data.list;
		$("#partener_t").tmpl({
			list : list
		}).appendTo($("#partenerList"));
		$(".swiper-slide").css("width", "140px");
	});
	ajaxPost({
		param : "{}"
	}, "/admin/news/list/about", function(data) {
		data = data.list;
		var aboutList = [], newsList = [], joinList = [], contactList = [];
		for ( var k in data) {
			if (data[k].category == "关于我们") {
				aboutList.push(data[k]);
			}
			if (data[k].category == "新闻资讯") {
				newsList.push(data[k]);
			}
			if (data[k].category == "加入我们") {
				joinList.push(data[k]);
			}
			if (data[k].category == "联系我们") {
				contactList.push(data[k]);
			}
		}
		$("#about_t").tmpl({
			name : "关于我们",
			list : aboutList
		}).appendTo($("#aboutList"));
		$("#about_t").tmpl({
			name : "新闻资讯",
			list : newsList
		}).appendTo($("#aboutList"));
		$("#about_t").tmpl({
			name : "加入我们",
			list : joinList
		}).appendTo($("#aboutList"));
		$("#about_t").tmpl({
			name : "联系我们",
			list : contactList
		}).appendTo($("#aboutList"));
	});
}
var categoryObj = {};
var notTopicObj = {};

function news() {
	ajaxPost({
		param : JSON.stringify({
			topic : "1",
		})
	}, "/admin/news/list/news", function(data) {
		data = data.list
		for ( var k in data) {
			var category = data[k].category;
			var list = categoryObj[category];
			if (!list) {
				list = [];
			}
			list.push(data[k]);
			categoryObj[category] = list;
		}
		$("#news_t").tmpl({
			list : categoryObj.news
		}).appendTo($("#mainNews"));
	});
	ajaxPost({
		param : JSON.stringify({
			topic : "0",
		}),
	}, "/admin/news/list/news", function(data) {
		data = data.list;
		for ( var k in data) {
			var category = data[k].category;
			var list = notTopicObj[category];
			if (!list) {
				list = [];
			}
			list.push(data[k]);
			notTopicObj[category] = list;
		}
		$("#newsList_t").tmpl({
			list : notTopicObj.news
		}).appendTo($("#newsList"));
		initHover();
	});
}
function initHover() {
	$(".newsRightItem").hover(function() {
		$(".newsRightItemNow").removeClass("newsRightItemNow");
		$(this).addClass("newsRightItemNow");
	});
	$(".nowNews").hover(function() {
		$(".nowNewsNow").removeClass("nowNewsNow");
		$(this).addClass("nowNewsNow");
	});
	console.log(123);
}
function newsShow(e, str) {
	$(e).parent().children().removeClass("newsHeadSelect");
	$(e).addClass("newsHeadSelect");
	$("#mainNews").empty();
	$("#news_t").tmpl({
		list : categoryObj[str]
	}).appendTo($("#mainNews"));
	$("#newsList").empty();
	$("#newsList_t").tmpl({
		list : notTopicObj[str]
	}).appendTo($("#newsList"));
	initHover();
}
function detail(id) {
	location.href = "detail.html?id=" + id;
}
function login() {
	// 记住密码
	var status = $("#rememberPass").is(':checked');
	if (status) {
		localStorage.petty_user = JSON.stringify({
			username : $("[attid=username]").val(),
			password : $("[attid=password]").val(),
			isRemember : 1
		});
	} else {
		localStorage.petty_user = JSON.stringify({
			isRemember : 0
		});
	}
	ajaxPost({
		username : $("[attid=username]").val(),
		password : $("[attid=password]").val(),
		type : "web"
	}, "/web/user/login", function(data) {
		if (data.code == 1) {
			location.href = "index.html";
		} else if (data.code == 10) {
			parent.layer.confirm('该账号已经在别的设备上登录，是否强制登录', {
				btn : [ '强制登录', '取消' ], // 按钮
				shade : false
			// 不显示遮罩
			}, function() {
				ajaxPost({
					username : $("[attid=username]").val(),
					password : $("[attid=password]").val(),
					type : "web",
					force : 1
				}, "/web/user/login", function(data) {
					location.href = "index.html";
				})
			});
		} else {
			alert(data.message);
		}
	})
}
news();
init();
